package de.ludo.output;

import java.util.Collection;
import java.util.Iterator;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.StreamMessage;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import de.ludo.dao.DAO;
import de.ludo.dao.DAOLocal;
import de.ludo.entities.Highscore;
import de.ludo.util.GameType;


/**
 * Message-Driven Bean implementation class for: OutputRequestProcessor
 *
 */
@MessageDriven(
		activationConfig = {  
			 @ActivationConfigProperty(
			      propertyName = "destinationType",
			      propertyValue = "javax.jms.Queue"),
			 @ActivationConfigProperty(
			      propertyName = "destination",
			      propertyValue = "queue/HighscoreQueue"),
			      })
public class OutputRequestProcessor implements MessageListener {
	
	String gametype;
	String username;
	int score;
	
	@EJB(beanName = "DAO", beanInterface = de.ludo.dao.DAOLocal.class)
	private DAOLocal dao;
	
	@Override
	public void onMessage(Message message) {
       try {
    	   StreamMessage streamMessage = (StreamMessage) message;
    	   gametype = streamMessage.getStringProperty("gametype");
    	   username = streamMessage.getStringProperty("username");
    	   score = streamMessage.getIntProperty("points");

    	   Highscore highscore = dao.findHighscoreByGameAndName(GameType.Ludo, username);
    	   highscore.setScore(score); 
    	   dao.updateHighscore(highscore);  
       }
       catch (JMSException e) {
            throw new EJBException(e);
       }
    }

}
